package com.lavegue.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.lavegue.common.BaseService;
import com.lavegue.common.Page;
import com.lavegue.dao.MsgDao;
import com.lavegue.model.EveryDayConsumptionAggregate;
import com.lavegue.model.Msg;
import com.lavegue.model.TopUp;
import com.lavegue.model.UserInfo;
import com.lavegue.service.MsgService;
import com.lavegue.service.TopUpService;
import com.lavegue.service.UserInfoService;

public class MsgServiceImpl extends BaseService<Msg, Integer, MsgDao> implements MsgService{

	private TopUpService topUpService;
	private UserInfoService userInfoService;

	/**20111226
	 * 通过用户ID查询短信消息
	 * @return
	 */
	public List<Msg> findMsgByUid(Integer uid)throws Exception {
		return this.getDao().findMsgByUid(uid);
	}

	/**ljp 20111227
	 * 查询发送者用的所有彩信信息
	 * @param pageNo    当前页数
	 * @param pageSize  每页多少条
	 * @param titleName 彩信标题
	 * @param beginTime 选择开始时间
	 * @param endTime   选择结束时间
	 * @param senderUid  发送者的用户ID
	 * @return
	 * @throws Exception 
	 */
	public Page<Msg> findMsgList(int pageNo, int pageSize, String titleName,
			Date beginTime, Date endTime, int senderUid) throws Exception {
		return this.getDao().findMsgList(pageNo,pageSize,titleName,beginTime,endTime,senderUid);
	}
	
	/**ljp 20111228
	 * 查询发送者用户的 每日消费汇总 信息
	 * @param pageNo    当前页数
	 * @param pageSize  每页多少条
	 * @param dateTime  选择时间
	 * @param senderUid  发送者的用户ID
	 * @return
	 */
	public Page<EveryDayConsumptionAggregate> findEveryDayConsumptionList(int pageNo, int pageSize,
			Date dateTime, int senderUid) throws Exception {

		return this.getDao().findEveryDayConsumptionList(pageNo, pageSize,
				dateTime, senderUid);
	}
	
	/**20111230
	 * 统计当天消费总和
	 */
	public BigDecimal getDtConsumptionMoney(int senderUid){
		return this.getDao().getDtConsumptionMoney(senderUid);
	}
	//tzf 首页模糊查询根据标题和内容
	public Page<Msg> findMsgByContent(int pageNo,int pageSize,int typeid,String likeName){
		return this.getDao().findMsgByContent(pageNo, pageSize,typeid,likeName);
	}

	public Page<Msg> findMsgByTopUp(int pageNo, int pageSize, int topupid) {
		return this.getDao().findMsgByTopUp(pageNo, pageSize, topupid);
	}
	
	public void save(Msg msg) {
		
		//更新充值表
		List<TopUp> topupList = this.topUpService.findByUser(msg.getSender().getId());
		TopUp topUp = topupList.get(0);
		topUp.setRemainNum(topUp.getRemainNum() - 1);
		topUpService.update(topUp);
		
		UserInfo user = userInfoService.findById(msg.getSender().getId());
		user.setLastSendMsg(new Date());
		user.setMsgCnt(user.getMsgCnt() - 1);
		userInfoService.update(user);
		
		msg.setTopUp(topUp);
		//更新用户表
		this.getDao().save(msg);
	}
	
	public TopUpService getTopUpService() {
		return topUpService;
	}

	public void setTopUpService(TopUpService topUpService) {
		this.topUpService = topUpService;
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public void updateForAfterSend(Msg msg, List<String> recevices) {
		//更新充值表
		List<TopUp> topupList = this.topUpService.findByUser(msg.getSender().getId());
		int remain = recevices.size();
		List<TopUp> updateTopupList= new ArrayList<TopUp>();
		while(remain>0) {
			for (TopUp topUp : topupList) {
				if (topUp.getRemainNum() >= remain) {
					topUp.setRemainNum(topUp.getRemainNum() - remain);
					remain = 0;
					updateTopupList.add(topUp);
				} else {
					remain = remain - topUp.getRemainNum();
					topUp.setRemainNum(0);
					updateTopupList.add(topUp);
				}
			}
		}
		for (TopUp topUp : updateTopupList) {
			topUpService.update(topUp);
		}
		//更新用户信息
		//userinfo.setBalance(userinfo.getBalance().subtract(SM_PRICE.multiply(new BigDecimal(sendMsgResult.size()))));
		UserInfo userInfo = msg.getSender();
		userInfo.setMsgCnt(userInfo.getMsgCnt() - recevices.size());
		userInfoService.update(userInfo);
	}
	
}
